package com.bocloud.cmp.driver.providers.vmware;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.LocationDriver;
import com.bocloud.cmp.driver.utils.AssembleTools;
import com.bocloud.cmp.model.VDCModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;

public class VmwareLocationDriver extends VmwareDriver implements LocationDriver {

	private static Logger logger = LoggerFactory.getLogger(VmwareLocationDriver.class);

	public VmwareLocationDriver() {
		super();
	}

	/**
	 * @param endpoint
	 * @param username
	 * @param password
	 */
	public VmwareLocationDriver(String endpoint, String username, String password) {
		super(endpoint, username, password);
	}

	@Override
	public BsmResult listRegions() {
		return null;
	}

	@Override
	public BsmResult sync() {
		List<VDCModel> dcList = Lists.newArrayList();
		try {
			ManagedEntity[] dcEntities = new InventoryNavigator(this.getRootFolder()).searchManagedEntities(DC);
			if (null != dcEntities) {
				for (ManagedEntity dcEntity : dcEntities) {
					VDCModel model = AssembleTools.getInstance().assembleDc(dcEntity);
					dcList.add(model);
				}
			}
		} catch (Exception e) {
			logger.error("sync info error:", e);
			return new BsmResult(false, "同步失败");
		} finally {
			this.getServiceInstance().getServerConnection().logout();
		}
		return new BsmResult(true, dcList, "success");
	}

	@Override
	public BsmResult listZones(String region) {
		return null;
	}

}
